import request from '@/utils/request';
import type { Store, StoreListData, StoreQuery, City, VenueType, VenueListData, Venue, VenueQuery } from '@/types/store';

/**
 * 获取门店列表
 * @param params 
 * @returns 
 */
export const fetchStores = (params: StoreQuery) => {
  return request<StoreListData>({
    url: '/v1/store',
    method: 'GET',
    data: params,
  });
};

/**
 * 获取门店详情
 * @param id 
 * @returns 
 */
export const fetchStoreDetail = (id: number) => {
  return request<Store>({
    url: `/v1/store/${id}`,
    method: 'GET',
  });
};

/**
 * 创建门店
 * @param data 
 * @returns 
 */
export const createStore = (data: Partial<Store>) => {
  return request<Store>({
    url: '/v1/store',
    method: 'POST',
    data,
  });
};

/**
 * 更新门店
 * @param id 
 * @param data 
 * @returns 
 */
export const updateStore = (id: number, data: Partial<Store>) => {
  return request<Store>({
    url: `/v1/store/${id}`,
    method: 'PUT',
    data,
  });
};

/**
 * 删除门店
 * @param id 
 * @returns 
 */
export const deleteStore = (id: number) => {
  return request<any>({
    url: `/v1/store/${id}`,
    method: 'DELETE',
  });
};

/**
 * 获取城市列表
 * @returns 
 */
export const fetchCities = () => {
  return request<{ list: City[] }>({
    url: '/v1/city',
    method: 'GET',
    data: { pageSize: 500, status: 1 }, // 获取所有启用的城市
  });
};

/**
 * 获取场地类型列表
 * @returns 
 */
export const fetchVenueTypes = () => {
  return request<VenueType[]>({
    url: '/v1/venue/type',
    method: 'GET',
  });
};

/**
 * 根据门店ID获取场地列表
 * @param storeId
 * @returns
 */
export const fetchVenuesByStore = (storeId: number) => {
  return request<VenueListData>({
    url: '/v1/venue',
    method: 'GET',
    data: { store_id: storeId, pageSize: 500 },
  });
};

/**
 * 批量获取场地信息
 * @param ids 场地ID列表
 */
/**
 * 获取场地列表
 * @param params
 * @returns
 */
export const fetchVenues = (params: VenueQuery) => {
  return request<VenueListData>({
    url: '/v1/venue',
    method: 'GET',
    data: params,
  });
};

/**
 * 获取场地详情
 * @param id 
 * @returns 
 */
export const fetchVenueDetail = (id: number) => {
  return request<Venue>({
    url: `/v1/venue/${id}`,
    method: 'GET',
  });
};

/**
 * 创建场地
 * @param data 
 * @returns 
 */
export const createVenue = (data: Partial<Venue>) => {
  return request<Venue>({
    url: '/v1/venue',
    method: 'POST',
    data,
  });
};

/**
 * 更新场地
 * @param id 
 * @param data 
 * @returns 
 */
export const updateVenue = (id: number, data: Partial<Venue>) => {
  return request<Venue>({
    url: `/v1/venue/${id}`,
    method: 'PUT',
    data,
  });
};

/**
 * 删除场地
 * @param id 
 * @returns 
 */
export const deleteVenue = (id: number) => {
  return request<any>({
    url: `/v1/venue/${id}`,
    method: 'DELETE',
  });
};

/**
 * 获取门店统计数据
 * @param id 门店ID
 * @returns 
 */
export const fetchStoreStats = (id: number) => {
  return request<{
    venueCount: number;
    todayBookings: number;
    activeVenues: number;
    utilizationRate: number;
  }>({
    url: `/v1/store/${id}/stats`,
    method: 'GET',
  });
};

/**
 * 创建城市
 * @param data 
 * @returns 
 */
export const createCity = (data: { name: string; sort?: number; status?: number }) => {
  return request<City>({
    url: '/v1/city',
    method: 'POST',
    data,
  });
};

/**
 * 检查场地可用性
 * @param venueId 场地ID
 * @param startTime 开始时间（时间戳）
 * @param endTime 结束时间（时间戳）
 * @returns 
 */
export const checkVenueAvailability = (venueId: number, startTime: number, endTime: number) => {
  return request<{ available: boolean; message?: string }>({
    url: '/v1/order/check_availability',
    method: 'GET',
    data: {
      venue_id: venueId,
      start_time: startTime,
      end_time: endTime,
    },
  });
};

